<template>
  <div class="profile-page">
    <!-- 个人资料封面 -->
    <div class="profile-cover">
      <div class="cover-image">
        <img src="https://picsum.photos/id/1025/1200/400" alt="封面图片" />
      </div>
      
      <!-- 用户信息头部 -->
      <div class="profile-header">
        <div class="profile-avatar">
          <img :src="user.avatar" alt="用户头像" />
        </div>
        <div class="profile-basic-info">
          <h1>{{ user.name }}</h1>
          <p class="username">@{{ user.username }}</p>
          <div class="profile-stats">
            <div class="stat-item">
              <span class="stat-number">10</span>
              <span class="stat-label">收藏文章</span>
            </div>
            <div class="stat-item">
              <span class="stat-number">5</span>
              <span class="stat-label">评论</span>
            </div>
            <div class="stat-item">
              <span class="stat-number">2</span>
              <span class="stat-label">关注</span>
            </div>
          </div>
        </div>
        
        <div class="profile-actions">
          <button class="edit-profile-button">编辑资料</button>
          <button @click="handleLogout" class="logout-button">退出登录</button>
        </div>
      </div>
    </div>
    
    <!-- 个人资料内容 -->
    <div class="profile-content">
      <div class="container">
        <!-- 左侧导航 -->
        <div class="profile-nav">
          <ul class="nav-list">
            <li class="nav-item active">
              <a href="#" class="nav-link">个人主页</a>
            </li>
            <li class="nav-item">
              <a href="#" class="nav-link">我的收藏</a>
            </li>
            <li class="nav-item">
              <a href="#" class="nav-link">我的评论</a>
            </li>
            <li class="nav-item">
              <a href="#" class="nav-link">设置</a>
            </li>
          </ul>
        </div>
        
        <!-- 右侧内容 -->
        <div class="profile-main">
          <!-- 用户基本信息卡片 -->
          <div class="info-card">
            <h3>基本信息</h3>
            <div class="info-item">
              <span class="label">用户ID:</span>
              <span class="value">{{ user.id }}</span>
            </div>
            <div class="info-item">
              <span class="label">用户角色:</span>
              <span class="value">{{ user.role }}</span>
            </div>
            <div class="info-item">
              <span class="label">注册日期:</span>
              <span class="value">2023-01-15</span>
            </div>
            <div class="info-item">
              <span class="label">宠物数量:</span>
              <span class="value">2只</span>
            </div>
          </div>
          
          <!-- 最近活动卡片 -->
          <div class="activity-card">
            <h3>最近活动</h3>
            <div class="activity-list">
              <div class="activity-item">
                <div class="activity-icon">
                  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M19 21H5C3.89543 21 3 20.1046 3 19V5C3 3.89543 3.89543 3 5 3H19C20.1046 3 21 3.89543 21 5V19C21 20.1046 20.1046 21 19 21Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M3 10H21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                </div>
                <div class="activity-content">
                  <p>收藏了文章 <a href="#">《如何正确训练你的狗狗》</a></p>
                  <span class="activity-time">2小时前</span>
                </div>
              </div>
              <div class="activity-item">
                <div class="activity-icon">
                  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                </div>
                <div class="activity-content">
                  <p>评论了文章 <a href="#">《狗狗健康饮食指南》</a></p>
                  <span class="activity-time">昨天</span>
                </div>
              </div>
              <div class="activity-item">
                <div class="activity-icon">
                  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M22 4L12 14.01l-3-3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                </div>
                <div class="activity-content">
                  <p>点赞了文章 <a href="#">《猫咪常见疾病预防》</a></p>
                  <span class="activity-time">3天前</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import authService from '../services/authService';

const router = useRouter();

// 计算属性获取当前登录用户
const user = computed(() => {
  const currentUser = authService.getCurrentUser();
  // 如果用户未登录，重定向到登录页面
  if (!currentUser) {
    router.push('/login');
  }
  return currentUser;
});

// 处理退出登录逻辑
const handleLogout = () => {
  authService.logout();
  // 退出后重定向到登录页面
  router.push('/login');
};
</script>

<style scoped>
/* 全局样式变量 */
:root {
  --primary-color: #ff7f00;
  --primary-hover: #e66f00;
  --text-primary: #333;
  --text-secondary: #666;
  --text-light: #999;
  --background-light: #f5f5f5;
  --white: #ffffff;
  --border-color: #e0e0e0;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.15);
  --radius: 8px;
  --transition: all 0.3s ease;
}

/* 个人资料页面整体样式 */
.profile-page {
  min-height: 100vh;
  background-color: var(--background-light);
}

/* 封面样式 */
.profile-cover {
  position: relative;
  width: 100%;
}

.cover-image img {
  width: 100%;
  height: 400px;
  object-fit: cover;
  display: block;
}

/* 个人资料头部 */
.profile-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 24px;
  padding: 0 24px;
  position: relative;
  bottom: 80px;
}

.profile-avatar {
  flex-shrink: 0;
  border-radius: 50%;
  overflow: hidden;
  border: 5px solid var(--white);
  width: 160px;
  height: 160px;
  box-shadow: var(--shadow);
}

.profile-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-basic-info {
  flex: 1;
  min-width: 250px;
}

.profile-basic-info h1 {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.username {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

/* 统计数据 */
.profile-stats {
  display: flex;
  gap: 24px;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: var(--primary-color);
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: var(--text-secondary);
}

/* 操作按钮 */
.profile-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.edit-profile-button {
  background-color: transparent;
  color: var(--primary-color);
  border: 2px solid var(--primary-color);
  border-radius: var(--radius);
  padding: 10px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.edit-profile-button:hover {
  background-color: var(--primary-color);
  color: var(--white);
}

.logout-button {
  background-color: var(--primary-color);
  color: var(--white);
  border: none;
  border-radius: var(--radius);
  padding: 10px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.logout-button:hover {
  background-color: var(--primary-hover);
}

/* 个人资料内容区域 */
.profile-content {
  position: relative;
  bottom: 60px;
  padding: 0 24px 40px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

/* 侧边导航 */
.profile-nav {
  flex: 0 0 250px;
  background-color: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  height: fit-content;
}

.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  margin-bottom: 8px;
}

.nav-link {
  display: block;
  padding: 12px 16px;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius);
  transition: var(--transition);
  font-weight: 500;
}

.nav-link:hover {
  background-color: rgba(255, 127, 0, 0.1);
  color: var(--primary-color);
}

.nav-item.active .nav-link {
  background-color: rgba(255, 127, 0, 0.1);
  color: var(--primary-color);
  font-weight: 600;
}

/* 主要内容区域 */
.profile-main {
  flex: 1;
  min-width: 300px;
}

/* 信息卡片 */
.info-card,
.activity-card {
  background-color: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 24px;
}

.info-card h3,
.activity-card h3 {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid var(--border-color);
}

/* 信息项 */
.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border-color);
}

.info-item:last-child {
  border-bottom: none;
}

.info-item .label {
  font-weight: 600;
  color: var(--text-secondary);
}

.info-item .value {
  color: var(--text-primary);
  font-weight: 500;
}

/* 活动列表 */
.activity-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-item {
  display: flex;
  gap: 16px;
  padding: 16px;
  border-radius: var(--radius);
  transition: var(--transition);
}

.activity-item:hover {
  background-color: rgba(0, 0, 0, 0.02);
}

.activity-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(255, 127, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary-color);
}

.activity-content p {
  margin: 0 0 4px 0;
  color: var(--text-primary);
}

.activity-content a {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
}

.activity-content a:hover {
  text-decoration: underline;
}

.activity-time {
  font-size: 14px;
  color: var(--text-light);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .container {
    flex-direction: column;
  }
  
  .profile-nav {
    flex: 1;
  }
}

@media (max-width: 768px) {
  .cover-image img {
    height: 250px;
  }
  
  .profile-header {
    bottom: 60px;
    padding: 0 16px;
  }
  
  .profile-avatar {
    width: 120px;
    height: 120px;
  }
  
  .profile-basic-info h1 {
    font-size: 24px;
  }
  
  .profile-stats {
    gap: 16px;
  }
  
  .stat-number {
    font-size: 20px;
  }
  
  .profile-actions {
    width: 100%;
    justify-content: center;
  }
  
  .profile-content {
    padding: 0 16px 32px;
  }
}

@media (max-width: 480px) {
  .profile-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .profile-basic-info {
    min-width: unset;
  }
  
  .profile-actions {
    flex-direction: column;
    width: 100%;
  }
  
  .edit-profile-button,
  .logout-button {
    width: 100%;
  }
}
</style>